package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/07 14:25
 */
@Service("comboFieldAnalyseServiceImpl")
public class ComboFieldAnalyseServiceImpl implements FieldAnalyseService {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO.getType(), FieldTypeEnum.RADIO_GROUP.getType());
    }

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntityExt, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, businessType, paasFormDataEntityExt.getCorpid(), formId);
            fieldAttrEntity.setItems(itemPoJos);
            return  ItemUtil.getCustomItemList(fieldAttrEntity, data);
        } else {
            return ItemUtil.getCustomItemList(fieldAttrEntity, data);
        }
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        String corpid = businessRuleInfoPojo.getCorpid();
        if ( Objects.equals(ConditionEnum.IN.getSymbol(), filterCondition.getSymbol())
                || Objects.equals(ConditionEnum.NOIN.getSymbol(), filterCondition.getSymbol())) {
            if (object instanceof String) {
                if (object.toString().contains(PaasConstant.CHINESE_SEMICOLON)) {
                    String[] valueArray = object.toString().split(PaasConstant.CHINESE_SEMICOLON);
                    return Arrays.asList(valueArray);
                } else {
                    List<ItemPoJo> itemPoJos = proFormHelp.getItem(thisField, businessRuleInfoPojo.getBusinessType(), corpid, businessRuleInfoPojo.getFormId());
                    thisField.setItems(itemPoJos);
                    return ItemUtil.getCustomItemValue(thisField, String.valueOf(object));
                }
            } else if (object instanceof Map || object instanceof ItemPoJo) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                object = itemPoJo.getValue();
            } else if (object instanceof List) {
                List<Object> list = JSON.parseArray(JSON.toJSONString(object), Object.class);
                List<Object> resultList = new ArrayList<>();
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(thisField, businessRuleInfoPojo.getBusinessType(), corpid, businessRuleInfoPojo.getFormId());
                thisField.setItems(itemPoJos);
                for (Object item : list) {
                    if (item instanceof Map || item instanceof ItemPoJo) {
                        ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(item), ItemPoJo.class);
                        if (Objects.equals(1, itemPoJo.getIsOther())) {
                            // 其他选项
                            Object value = itemPoJo.getValue();
                            if (value instanceof String && value.toString().contains(PaasConstant.CHINESE_SEMICOLON)) {
                                // 字符串类型，用符号分割
                                String[] valueArray = value.toString().split(PaasConstant.CHINESE_SEMICOLON);
                                resultList.add(Arrays.asList(valueArray));
                            } else if (value instanceof List) {
                                // list类型，无需分割，直接使用
                                List<Object> valueList = JSON.parseArray(JSON.toJSONString(value));
                                resultList.addAll(valueList);
                            } else {
                                // 字符串，即单个选项
                                resultList.add(itemPoJo.getValue());
                            }
                        } else {
                            resultList.add(itemPoJo.getValue());
                        }
                    } else {
                        resultList.add(ItemUtil.getCustomItemValue(thisField, String.valueOf(item)));
                    }
                }
                return resultList;
            } else {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261023, BusinessRuleErrorCodeEnum.API_ERROR_261023.getMsg() , thisField.getAttrName());
            }
        } else if (Objects.equals(ConditionEnum.LIKE.getSymbol(), filterCondition.getSymbol())
                || Objects.equals(ConditionEnum.NOLIKE.getSymbol(), filterCondition.getSymbol())) {
            return object;
        } else {
            if (object instanceof String) {
                if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), thisField.getComboType())) {
                    List<ItemPoJo> itemPoJos = proFormHelp.getItem(thisField, businessRuleInfoPojo.getBusinessType(), corpid, businessRuleInfoPojo.getFormId());
                    thisField.setItems(itemPoJos);
                    object = ItemUtil.getCustomItemValue(thisField, String.valueOf(object));
                }
            } else if (object instanceof Map || object instanceof ItemPoJo) {
                if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), thisField.getComboType())) {
                    ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                    object = itemPoJo.getValue();
                }
            }
        }
        return object;
    }
}
